package com.ct.application.shipping.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.ct.application.shipping.model.po.LiuChengDingYiBuZhouSavePO;
import com.ct.application.shipping.model.po.LiuChengDingYiSavePO;
import com.ct.application.shipping.model.po.LiuChengDingYiSearchPO;
import com.ct.application.shipping.model.vo.LiuChengDingYiVO;
import com.ct.application.shipping.model.vo.LiuChengLeiXingVO;
import com.ct.application.shipping.model.vo.LiuChengVO;
import com.ct.application.shipping.model.vo.LiuChengXiangQingVO;
import com.ct.application.shipping.service.LiuChengDingYiService;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.shipping.mapper.LiuChengDingYiMapper;
import com.ct.shipping.orm.LiuChengDingYiDO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 流程定义表 服务实现类
 * </p>
 *
 * @author carlo
 */
@Service
public class LiuChengDingYiServiceImpl extends BaseServiceImpl<LiuChengDingYiMapper, LiuChengDingYiDO>
        implements LiuChengDingYiService {


    /**
     * 查询流程定义
     */
    @Override
    public LiuChengVO findLiuChengDingYi(LiuChengDingYiSearchPO searchPO) {
        String leiXing = searchPO.getLeiXing();
        if (StringUtils.isEmpty(leiXing)) {
            leiXing = "离船申请";
        }

        LiuChengVO liuChengVO = new LiuChengVO();
        //流程类型集合数据
        List<LiuChengLeiXingVO> liuChengLeiXingVOS = Lists.newArrayList();
        //流程定义集合数据
        List<LiuChengDingYiVO> liuChengDingYiVOS = Lists.newArrayList();

        List<LiuChengDingYiDO> lcsqList = this.lambdaQuery().eq(LiuChengDingYiDO::getLiuChengLeiXing, "离船申请").list();
        List<LiuChengDingYiDO> cydlList = this.lambdaQuery().eq(LiuChengDingYiDO::getLiuChengLeiXing, "船员调令").list();
        liuChengLeiXingVOS.add(
                new LiuChengLeiXingVO().setLiuChengLeiXing("离船申请").setLiuChengShuLiang(lcsqList.size())
        );
        liuChengLeiXingVOS.add(
                new LiuChengLeiXingVO().setLiuChengLeiXing("船员调令").setLiuChengShuLiang(cydlList.size())
        );

        List<LiuChengDingYiDO> liuChengDingYiList = new ArrayList<>();
        if ("离船申请".equals(leiXing)) {
            liuChengDingYiList = lcsqList;
        }
        if ("船员调令".equals(leiXing)) {
            liuChengDingYiList = cydlList;
        }
        for (LiuChengDingYiDO liuChengDingYiDO : liuChengDingYiList) {
            LiuChengDingYiVO liuChengDingYiVO = new LiuChengDingYiVO();
            liuChengDingYiVO.setId(liuChengDingYiDO.getId());
            liuChengDingYiVO.setMingCheng(liuChengDingYiDO.getMingCheng());
            if (liuChengDingYiDO.getChuanBoMing() != null) {
                liuChengDingYiVO.setChuanBoMing(liuChengDingYiDO.getChuanBoMing().replace(",", "、"));
            }
            if (liuChengDingYiDO.getZhiWeiMing() != null){
                liuChengDingYiVO.setZhiWeiMing(liuChengDingYiDO.getZhiWeiMing().replace(",", "、"));
            }
            if (liuChengDingYiDO.getBuZhouShuJu() != null) {
                List<LiuChengDingYiBuZhouSavePO> buZhouList = JSONArray.parseArray(liuChengDingYiDO.getBuZhouShuJu(), LiuChengDingYiBuZhouSavePO.class);
                if(CollectionUtils.isNotEmpty(buZhouList)) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < buZhouList.size(); i++) {
                        LiuChengDingYiBuZhouSavePO buZhou = buZhouList.get(i);
                        if (i == (buZhouList.size() - 1)) {
                            sb.append(buZhou.getJueSeMing());
                        } else {
                            sb.append(buZhou.getJueSeMing()).append(" - ");
                        }
                    }
                    liuChengDingYiVO.setBuZhouShuJu(sb.toString());
                }
            }
            liuChengDingYiVOS.add(liuChengDingYiVO);
        }
        liuChengVO.setLiuChengLeiXingVOS(liuChengLeiXingVOS);
        liuChengVO.setLiuChengDingYiVOS(liuChengDingYiVOS);
        return liuChengVO;
    }


    /**
     * 新增流程定义
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveLiucChengDingyi(LiuChengDingYiSavePO savePO){
        savePO.setId(null); //新增不需要id
        if (StringUtils.isEmpty(savePO.getMingCheng())) {
            throw new RuntimeException("流程名称不能为空");
        }
        LiuChengDingYiDO dingYiDO = this.lambdaQuery()
                .eq(LiuChengDingYiDO::getMingCheng, savePO.getMingCheng())
                .one();
        if (dingYiDO != null) {
            throw new RuntimeException("《"+savePO.getMingCheng()+"》流程名称已存在，请更换");
        }

        List<String> chuanBoMingList = savePO.getChuanBoMingList();
        List<String> zhiWeiMingList = savePO.getZhiWeiMingList();
        List<LiuChengDingYiBuZhouSavePO> buZhouList = savePO.getBuZhouList();
        if (chuanBoMingList == null || chuanBoMingList.size() == 0) {
            throw new RuntimeException("船舶数据不能为空");
        }
        if (zhiWeiMingList == null || zhiWeiMingList.size() == 0) {
            throw new RuntimeException("职位数据不能为空");
        }
        if (buZhouList == null || buZhouList.size() == 0) {
            throw new RuntimeException("流程步骤数据不能为空");
        }

        String chuanBoMingStr = StringUtils.join(chuanBoMingList, ",");
        String zhiWeiMingStr = StringUtils.join(zhiWeiMingList, ",");

        LiuChengDingYiDO liuChengDingYiDO = new LiuChengDingYiDO();
        liuChengDingYiDO.setMingCheng(savePO.getMingCheng());
        liuChengDingYiDO.setBeiZhu(savePO.getBeiZhu());
        liuChengDingYiDO.setLiuChengLeiXing(savePO.getLiuChengLeiXing());
        liuChengDingYiDO.setChuanBoMing(chuanBoMingStr);
        liuChengDingYiDO.setZhiWeiMing(zhiWeiMingStr);
        liuChengDingYiDO.setBuZhouShuJu(JSONArray.toJSONString(buZhouList));
        return liuChengDingYiDO.insert();
    }

    /**
     * 查看 流程定义 详情数据
     */
    @Override
    public LiuChengXiangQingVO getLiucChengDingyi(Long id){
        LiuChengDingYiDO liuChengDingYiDO = getById(id);
        LiuChengXiangQingVO liuChengXiangQingVO = tranferBo(liuChengDingYiDO, LiuChengXiangQingVO.class);

        //船舶集合数据
        String chuanBoMing = liuChengDingYiDO.getChuanBoMing();
        List<String> chuanBoMingList= List.of(chuanBoMing.split(","));
        //职位集合数据
        String zhiWeiMing = liuChengDingYiDO.getZhiWeiMing();
        List<String> zhiWeiMingList = List.of(zhiWeiMing.split(","));
        //流程步骤对象数据
        String buZhouShuJu = liuChengDingYiDO.getBuZhouShuJu();
        List<LiuChengDingYiBuZhouSavePO> buZhouList = JSONArray.parseArray(buZhouShuJu, LiuChengDingYiBuZhouSavePO.class);

        liuChengXiangQingVO.setChuanBoMingList(chuanBoMingList);
        liuChengXiangQingVO.setZhiWeiMingList(zhiWeiMingList);
        liuChengXiangQingVO.setBuZhouList(buZhouList);
        return liuChengXiangQingVO;
    }


    /**
     * 修改流程定义
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateLiucChengDingyi(LiuChengDingYiSavePO savePO){
        if (savePO.getId() == null) {
            throw new RuntimeException("id不能为空");
        }
        if (StringUtils.isEmpty(savePO.getMingCheng())) {
            throw new RuntimeException("流程名称不能为空");
        }
        LiuChengDingYiDO dingYiDO = this.lambdaQuery()
                .eq(LiuChengDingYiDO::getMingCheng, savePO.getMingCheng())
                .ne(LiuChengDingYiDO::getId, savePO.getId())
                .one();
        if (dingYiDO != null) {
            throw new RuntimeException("《"+savePO.getMingCheng()+"》流程名称已存在，请更换");
        }

        List<String> chuanBoMingList = savePO.getChuanBoMingList();
        List<String> zhiWeiMingList = savePO.getZhiWeiMingList();
        List<LiuChengDingYiBuZhouSavePO> buZhouList = savePO.getBuZhouList();

        String chuanBoMingStr = StringUtils.join(chuanBoMingList, ",");
        String zhiWeiMingStr = StringUtils.join(zhiWeiMingList, ",");

        LiuChengDingYiDO liuChengDingYiDO = new LiuChengDingYiDO();
        liuChengDingYiDO.setId(savePO.getId());
        liuChengDingYiDO.setMingCheng(savePO.getMingCheng());
        liuChengDingYiDO.setBeiZhu(savePO.getBeiZhu());
        liuChengDingYiDO.setChuanBoMing(chuanBoMingStr);
        liuChengDingYiDO.setZhiWeiMing(zhiWeiMingStr);
        liuChengDingYiDO.setBuZhouShuJu(JSONArray.toJSONString(buZhouList));
        return liuChengDingYiDO.updateById();
    }


    /**
     * 删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delLiucChengDingyi(Long id){
        return this.removeById(id);
    }




}
